University of South Florida
git bash for windows from:Ctrl + Shift + PImportant
You should also create an account on GitHub. Optionally, register with USF for a student previleges.
Check if terminal understands git command. Type git on terminal and confirm no error message.
Make sure you are able to login to Github.
Distributed version control system, created by Linus Torvalds (2005)
“Track changes” made by team members, merge into main, etc.
Considered a “must” for software development, and many data science projects.
Has a learning curve, but it’s worth learning even for solo projects.
An online hosting platform that is based on Git system
Easy to browse other repositories (public, free)
Others: GitLab, Bitbucket, GitBucket, etc…
Version control: track changes, revert to previous versions
Collaboration: multiple people can work on the same project
Backup: store your project on the cloud
Portfolio: showcase your work to potential employers
Open source: contribute to other projects
Nope.
Git can be done completely locally (w/o internet).
GitHub deploys Git to online cloud system.
Git is primarily designed to handle text files and tracks changes line by line.
It can upload binary files (e.g. images, pdfs), but doesn’t track differences like text files.
GitHub has a small file size limit (100MB)
It is intended for code tracking. Large data should be stored elsewhere.
On top of its practicality, it gives you hands-on experience & knowledge of cryptographic Merkle Tree algorithm.
Blockchains:
git init is used to initialize a new git repository from current working directory.
git status shows the status of changes as untracked, modified, or staged.
git status provides information about the state of your working directory and staging area. Here are the key terms:
Untracked files: Files that Git isn’t tracking yet.
Tracked files: Files that Git has been monitoring for changes.
git add).git log shows the history of commits and its corresponding hashes.
:q to quit!git add stages files for commit.
git reset unstages all files and folders you added.
git commit creates your project’s version, or a hash block.
You should provide message folling -m, that describes the version.
Go to your folder from terminal.
In the Shell folder, create a text file For_first_commit.sh.
In the file, add text “Learing Git is Fun!”
Get out of Shell folder, and back to your class folder.
Now initiate git in your class folder.
git status. What do you see?git add . to stage all untracked files and folders.Now let’s make a commit.
For commit message, use “My first commit”.
git reset also can reset your previous commit.
HEAD is a pointer where you are currently at.HEAD~1 means 1 commit before current hash (version).git reset Bgit reset --mixed Bgit reset --soft Bgit reset --hard BGo to your folder from terminal.
In the Shell folder, edit For_first_commit.sh file:
Get out of Shell folder, and back to your class folder.
Then check the status. What do you see?
Remember this status. We will come back to here after reset.
Add all changes to the staging area.
Commit with message “Test second commit”
Check the status.
Check the commit logs with git log or git log --oneline.
git reset HEAD~1git reset (hash)git add .git reset --hard HEAD~1.The process so far is only for your local version management, which is also completely fine for local work.
However, you can also make your repository “Synchronized” to a cloud server: Github.
git remote add adds remote repository.
origin is naming convention for Github remote repo.main is your local branch name.Let’s check the branch names first.
Let’s check the current local branch name. We use main branch named as “main”.
If your local branch name is not main (i.e. master or etc), change it to main with below command.
Now we can add remote repository with following command.
Go to the github website
Choose visibility to public
Name your repository nicely for this classroom
DO NOT ADD README FILE (leave it unchecked)
Copy the address to a text file
From now on, you can browse your remote branches with
git push sends your commit to the remote repository.
Let’s push your local classrom git to the github.
Check first: 1) Your local status is clean (check with git status) 2) If not clean (You made edit after the initial version) stage changes and commit.
Then type below to push:
Browse your github site, and see files are uploaded.
https://github.com/yourGithubId/Reponame.git
Note
By default, git will only track files, not empty folders. Folders are tracked when files are in it.
What if you wanted to download someone else’s remote repository to your local machine? Git clone is for the first time downloading from the remote repository.
Now let’s clone OUR CLASS repository (that I upload all class materials).
git clone action will create a folder and download everything from the upstream.
Example: if your class folder address is ~/my_fin4770 then you should clone it from ~.
git pull downloads and apply changes from the repote repository.
Note
Actually, git pull does two things altogether:
Now, I will make modify and update the class repositroy.
After the update, use git pull from the local class repo to apply the update.
When using github from other apps (e.g. Positron, VScode, etc), you’ll use PAT (personal access token) instead of password.
Generate token from the github page, possibly with no expiration (or long enough)
By default, git pull is
git pull has three methods:
git pull --merge (default)
git pull --rebase
git pull --ff-only
git mergeCombines changes into a new merge commit. You will need to combine and resolve conflicts.
Content of file.txt
Alice edits the second line and commits and push to main first:
Bob also edits the same line, but did not pull Alice’s changes. He edits and commits locally:
When Bob tries to push changes with git push, Git will reject because his local branch is behind the remote.
Bob runs git pull which is git fetch and git merge: git shows the conflicts
The file.txt now contains conflict markers
To resolve conflict, Bob should modify the file manually.
Bob stages this file and commits the resolution:
After resolving, Bob pushes with git push.
git rebaseGit replays your local branch on top of the remote branch, creating a linear history:
Content of file.txt
Alice edits the second line and commits and push to main first:
Bob also edits the same line, but did not pull Alice’s changes. He edits and commits locally:
When Bob tries to push changes with git push, Git will reject because his local branch is behind the remote.
Bob runs git pull --rebase which is git fetch and git rebase:
Instead of creating a merge commit,
git rewinds Bob’s local commit C
applies Alice’s changes from remote (origin/main) B
reapplies C on top of B
When there’s no conflict, (i.e., they did not change the same line of code) git pull –-rebase won’t raise any conflict message.
However, in our scenario, there’s conflict since both modified the same line, so:
rebase is halted
should be continued after conflict resolution
Similar to merge, conflict should be resolved manually.
The file.txt now contains conflict markers
To resolve conflict, Bob should modify the file manually.
Bob stages this file and continues the rebase (no commit)
After that Bob pushes with git push. The history is linear.
git merge --ff-onlyUsed when you expect no conflicts.
When you want linear history and want to move your local branch pointer forward without modifying.
Git keep tracks all changes within the project directory
New file, folder
Modifications (changes or updates)
Deletion
If there are files/folder you don’t want to track, specify them in .gitignore file.
If you want to remove untracked (not unstaged!) files:
FIN4770: Programming for FinTech